<html>
<head><meta charset="utf-8"><title>Proposals for macros for attributes? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Proposals.20for.20macros.20for.20attributes.3F.html">Proposals for macros for attributes?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212235358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Proposals%20for%20macros%20for%20attributes%3F/near/212235358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Proposals.20for.20macros.20for.20attributes.3F.html#212235358">(Oct 04 2020 at 18:34)</a>:</h4>
<p>Have there been any proposals for macros that expand to attributes that went anywhere?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">macro_rules</span><span class="o">!</span><span class="w"> </span><span class="n">Things</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">()</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cp">#[derive(Debug, Default)]</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="n">Things</span><span class="o">!</span><span class="p">{}</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">X</span><span class="p">;</span><span class="w"></span>
</code></pre></div>

<p>Or better, something like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">macro_rules</span><span class="o">!</span><span class="w"> </span><span class="n">Things2</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">()</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">derive</span><span class="p">(</span><span class="n">Debug</span><span class="p">,</span><span class="w"> </span><span class="nb">Default</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[Things2!{}]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Y</span><span class="p">;</span><span class="w"></span>
</code></pre></div>

<p>My custom derive macros want to have some configuration, but I could see people wanting to use that configuration across all uses of the derive in their crate. It'd be nice to offer some way for them to DRY it up without them writing their <em>own</em> macros :-)</p>



<a name="212243073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Proposals%20for%20macros%20for%20attributes%3F/near/212243073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Proposals.20for.20macros.20for.20attributes.3F.html#212243073">(Oct 04 2020 at 21:44)</a>:</h4>
<p>An alternative is to define the macro as:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">macro_rules</span><span class="o">!</span><span class="w"> </span><span class="n">MyDerives</span><span class="w"> </span><span class="p">{(</span><span class="w"></span>
<span class="w">    </span><span class="cp">$item</span>:<span class="nc">item</span><span class="w"></span>
<span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[derive(Debug, Default)]</span><span class="w"></span>
<span class="w">    </span><span class="cp">$item</span><span class="w"></span>
<span class="p">)}</span><span class="w"></span>
</code></pre></div>

<p>And then use it with <a href="https://docs.rs/macro_rules_attribute">https://docs.rs/macro_rules_attribute</a>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[macro_rules_attribute(MyDerives!)]</span><span class="w"></span>
<span class="cp">#[...]</span><span class="w"></span>
<span class="sd">/// ...</span>
<span class="k">struct</span> <span class="o">..</span><span class="p">.</span><span class="w"></span>
</code></pre></div>



<a name="212243285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Proposals%20for%20macros%20for%20attributes%3F/near/212243285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Proposals.20for.20macros.20for.20attributes.3F.html#212243285">(Oct 04 2020 at 21:50)</a>:</h4>
<p>But a very desirable feature indeed would be to be able to alias <em>inner</em> attributes, as a shorthand for long lint lists (clippy, or default one):</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![my_inner_attrs!()]</span><span class="w"></span>
</code></pre></div>

<p>or something like that</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>